/*************************************************************************
  > File Name: stumodel.c
  > Author: QiyangXia
  > Mail: 834061456@qq.com 
  > Created Time: 2021年12月31日 星期五 17时13分53秒
 ************************************************************************/
#include "main.h"


struct myclass *create_stuinfo_mem(int maxsize)
{
	struct myclass *pcls = NULL;
	if(maxsize <= 0)
	{
		printf("Input param is invalid\n");
		return NULL;
	}
	pcls = (struct myclass*)malloc(sizeof(struct myclass));
    if(NULL==pcls)
    {
        printf("Malloc failed\n");
        return NULL;
    }
	//assert(pcls);
	memset(pcls,0,sizeof(struct myclass));
	pcls->max = maxsize;
	pcls->pt = (struct stu*)malloc(sizeof(struct stu)*maxsize);
    if(NULL==pcls->pt)
    {
        printf("malloc failed\n");
        free(pcls);
        pcls=NULL;
        return NULL;
    }
	memset(pcls->pt,0,maxsize*sizeof(struct stu));
    pcls->cnt = 0;

	return pcls;
}

int destroy_stuinfo_mem(struct myclass *pcls)
{
    free(pcls->pt);
    pcls->pt=NULL;
	free(pcls);
    pcls=NULL;
	return 0;
}

int insert_student(struct myclass *pcls,struct stu pt){
	if( pcls->max == pcls->cnt)
	{
		printf("班级人数已满，无法插入\n");
		return 1;
	}
	pcls->pt[pcls->cnt++] = pt;
}

void printdata(struct myclass *pcls)
{
	printf("学号\t\t名字\t\t性别\t数学成绩\t英语成绩\t中文成绩\n");
	for(int i = 0;i< pcls->cnt;i++)
	{
		printf("%d\t%8s\t\t%s\t%f\t%f\t%f\n",
				pcls->pt[i].no,
				pcls->pt[i].name,
				pcls->pt[i].sex,
				pcls->pt[i].math,
				pcls->pt[i].english,
				pcls->pt[i].chinese);
	}
}
int search_no(struct myclass *pcls, int no) 
{
	for (int i = 0; i < pcls->cnt; i++) 
	{
		if (pcls->pt[i].no == no) 
		{
			return i;
		}
	}
	return -1;
}
int search_name(struct myclass *pcls,char *ename) 
{
	system("clear");
	for (int i = 0; i < pcls->cnt; i++) 
	{
		if (strcmp(pcls->pt[i].name, ename) == 0) 
		{	
			printf("\n\n找到了！！！学生信息如下\n");
			printf("%d\t%8s\t\t%s\t%f\t%f\t%f\n",
				pcls->pt[i].no,
				pcls->pt[i].name,
				pcls->pt[i].sex,
				pcls->pt[i].math,
				pcls->pt[i].english,
				pcls->pt[i].chinese);
			return i;
		}
	}
	printf("\n\n并为找到此学生信息！！\n\n\n");
	return -1;
}



void del_data(struct myclass *pcls, int a)
{
	if (a == -1) 
	{
		system("clear");
		printf("\n\n\n删除失败，无当前学号的学生!\n");
		return;
	}
	for (int i = a; i < pcls->cnt; i++) 
	{
		pcls->pt[i] = pcls->pt[i + 1];
	}
	pcls->cnt--;
	system("clear");
	printf("\n\n\n删除成功!\n");
}

void change_data(struct myclass *pcls)
{
	int num = 0;
	int searno = 0;
	system("clear");
	printf("\n\n请输入学生学号:");
	scanf("%d", &num);
	while(getchar()!='\n'){}
	searno = search_no(pcls, num);
	if ( searno== -1)
	{

		system("clear");
		printf("\n\n未找到相关信息!\n");
	}
	else
	{
		system("clear");
		printf("\n\n请输入学号，姓名，性别，数学成绩，英语成绩，中文成绩：");  
		scanf("%d%s%s%f%f%f",
				&pcls->pt[searno].no,
				pcls->pt[searno].name,
				pcls->pt[searno].sex,
				&pcls->pt[searno].math,
				&pcls->pt[searno].english,
				&pcls->pt[searno].chinese);
		while(getchar()!='\n'){}  
		printf("\n\n修改成功！\n");
	}
}

void bubble_sort_english(struct myclass *pcls) 
{
	int i = 0;
	int j = 0;
	for (i = 0; i < pcls->cnt; i++) 
	{
		for (j = 0; j < pcls->cnt - 1 - i; j++) 
		{
			if (pcls->pt[j].english > pcls->pt[j + 1].english) 
			{
				struct stu temp = pcls->pt[j];
				pcls->pt[j] = pcls->pt[j + 1];
				pcls->pt[j + 1] = temp;
			}
		}
	}
}

void bubble_sort_chinese(struct myclass *pcls)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < pcls->cnt; i++) 
	{
		for (j = 0; j < pcls->cnt - 1 - i; j++)
		{
			if (pcls->pt[j].chinese > pcls->pt[j + 1].chinese) 
			{
				struct stu temp = pcls->pt[j];
				pcls->pt[j] = pcls->pt[j + 1];
				pcls->pt[j + 1] = temp;
			}
		}
	}
}

void sel_sort_math(struct myclass *pcls)
{
	int i = 0;
	int min = 0;
	int j = 0;

	for (i = 0; i < pcls->cnt; i++) 
	{
		min = i;
		for(j = i + 1; j < pcls->cnt; j++)
		{
			if(pcls->pt[j].math < pcls->pt[min].math)
			{
				min = j;
			}
		}
		if(min != i)
		{
			struct stu temp = pcls->pt[min];
			pcls->pt[min] = pcls->pt[i];
			pcls->pt[i] = temp;
		}
	}
}

void insert_sort_sum(struct myclass *pcls)
{
	int i = 0;
	int j = 0;
	int sum = 0;
	for(i = 1; i < pcls->cnt; i++)
	{
		struct stu temp = pcls->pt[i];
		sum = pcls->pt[i].math + pcls->pt[i].english + pcls->pt[i].chinese;
		for(j = i-1; (j >= 0) && ((pcls->pt[j].math + pcls->pt[j].english + pcls->pt[j].chinese) > sum); j--)
		{
			pcls->pt[j + 1] = pcls->pt[j];
		}
		pcls->pt[j + 1] = temp;
	}
}
